package com.neuedu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
@RefreshScope
public class NacosConfigController {

    //1、硬编码方式
    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @GetMapping("/test-config1")
    public String nacosConfigTest1() {
        return applicationContext.getEnvironment().getProperty("config.appName");
    }

    //2、动态读取方式
    @Value("${config.appName}")
    private String appName;

    //
    @GetMapping("/test-config2")
    public String nacosCongifTest2() {
        return appName;
    }

    @Value("${config.env}")
    private String env;

    @GetMapping("/test-config3")
    public String nacosConfigTest3() {
        return env;
    }
}